package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetMealMapper setMealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
//        加入购物车不是简单的insert语句
//        用户每次添加购物车都有两种情况
//        第一种是往购物车里添加原本没有的商品
//        第二种情况是往购物车里添加原有的商品,这就得把对应商品的数量加一
//        先判断购物车的商品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        System.out.println(shoppingCart.getDishFlavor() + "rrrrrrrrrrrrrrrrrrrrrr");
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        System.out.println("ttttttttttttttttttttttttttt");
//        如果购物车里还没有这个商品,就直接往数据库插入一条数据
        if (list == null || list.isEmpty()) {
            shoppingCart.setNumber(1);
//            还没这么简单,shoppingcart还有很多字段得去查
//            首先,判断一下新加入的商品是菜品还是套餐
            if (shoppingCartDTO.getDishId() != null) {
//                查一下菜品的数据
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());

            } else {

                Setmeal setmeal = setMealMapper.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            shoppingCartMapper.save(shoppingCart);
        }
//        否则就把对应的商品数量加一
        else {
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        }
    }

    @Override
    public List<ShoppingCart> list() {
//        先拿到当前用户的id
        Long userId = BaseContext.getCurrentId();
//        根据用户id查询Ta的购物车
        return shoppingCartMapper.seleteByUserId(userId);
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
         ShoppingCart shoppingCart=shoppingCartMapper.getByDTO(shoppingCartDTO);
         if(shoppingCart.getNumber()==1){
             shoppingCartMapper.delete(shoppingCart);
         }
         else {
             shoppingCart.setNumber(shoppingCart.getNumber() - 1);
             shoppingCartMapper.update(shoppingCart);
         }
    }
}
